
#include "CGameApplication.h"

CGameApplication* CGameApplication::pGameInstance = NULL;
CGameApplication* CGameApplication::getInstance ()
{
	if (pGameInstance == NULL)  
	{
		pGameInstance = new CGameApplication();
	}
	
	return pGameInstance; 
}

void CGameApplication::initializeApplication()
{
	initializeApplicationVideo();
	initializeApplicationImages();
	initializeApplicationFonts();
}

void CGameApplication::initializeApplicationVideo()
{
	if ( SDL_Init(SDL_INIT_VIDEO) != 0 ) {
		printf("Unable to initialize SDL: %s\n", SDL_GetError());
		exit(1);
	}

	//const SDL_VideoInfo* myPointer = SDL_GetVideoInfo(); //Return a struct with information about the actual video mode

	int width = 1280; //Width and height for testing
	int height = 720;

	SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 ); //Establish a mode about how OpenGL it's going to work
	GApp->setMainSurface( SDL_SetVideoMode( width, height, 32, SDL_OPENGL) ); //Create the surface for the game 
	//SDL_ShowCursor(0);
	TTF_Init();

	//----------------------------------------------
	//----Initialize OpenGL for 2D------------------

	glEnable( GL_TEXTURE_2D );
 
	glClearColor( 0.5f, 0.5f, 0.5f, 0.0f );

	glViewport( 0, 0, width, height );
	 
	glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
	 
	glMatrixMode( GL_PROJECTION );
	glLoadIdentity();
	 
	glOrtho(0.0f, width, height, 0.0f, -1.0f, 1.0f);
	 
	glMatrixMode( GL_MODELVIEW );
	glLoadIdentity();

	//----------------------------------------------
	//----------------------------------------------
}

void CGameApplication::initializeApplicationImages()
{
	/*int width = GApp->getMainSurface()->w;
	int height = GApp->getMainSurface()->h;

	GLfloat px = (width/1680.f); //Proportion of the screen width
	GLfloat py = (height/1050.f); //Proportion of the screen height

	//Basic process for storage information about a texture and image.....Soon will be improvised
	Image *newImage = new Image(); 
	newImage->setID(0);	newImage->setTexture( CImageM->imageToGLTexture("ac-bro.jpg") );
	newImage->setPosX(100.f*px);   newImage->setPosY(100.f*py);
	newImage->setOffsetX( (GLfloat(width)) - (100.f*px)); newImage->setOffsetY( (GLfloat(height)) - (100.f*py));

	GApp->addImage(newImage); //Add to the vector of the images of the game*/

	int width = GApp->getMainSurface()->w;
	int height = GApp->getMainSurface()->h;

	Image *newImage = new Image(); 
	newImage->setID(0);	
	newImage->setFilename("map.png");
	newImage->setTexture( CImageM->imageToGLTexture("map.png") );
	newImage->setWidth(800.f);   newImage->setHeight(512.f);
	newImage->setPosX(250.f);   newImage->setPosY(100.f);
	newImage->setOffsetX( (250.f) + 800.f ); newImage->setOffsetY( (100.f) + 512.f );
	newImage->setIsActive(false);

	GApp->addImage(newImage); 

	newImage->setID(1);	
	newImage->setFilename("tilesheet.png");
	newImage->setTexture( CImageM->imageToGLTexture("tilesheet.png") );
	newImage->setWidth(96.f);   newImage->setHeight(64.f);
	newImage->setPosX(250.f);   newImage->setPosY(100.f);
	newImage->setOffsetX( (250.f) + 32.f ); newImage->setOffsetY( (100.f) + 32.f);
	newImage->setIsActive(false);

	GApp->addImage(newImage);

	Tile *tileTemp = new Tile(0, false);

	GApp->addTile(tileTemp);

	Sprite *sprPlayer = new Sprite();

	sprPlayer->addTexture( CImageM->imageToGLTexture("pacman.png") );
	sprPlayer->setPosX(200.f);   sprPlayer->setPosY(100.f);
	sprPlayer->setHeight(32.f);  sprPlayer->setWidth(32.f);
	sprPlayer->setOffsetX( (200.f) + 32.f); sprPlayer->setOffsetY( (100.f) + 32.f);
	sprPlayer->setSpeedX(sprPlayer->getWidth()/2);  sprPlayer->setSpeedY(sprPlayer->getHeight()/2);

	GApp->getPlayer()->setSpritePlayer(sprPlayer);

	delete newImage;
	delete sprPlayer;
}

void CGameApplication::initializeApplicationFonts()
{
	SDL_Color color;

	int sizeFont = 15;

	color.r = color.g = color.b = 255;

	Font *f = new Font(TTF_OpenFont("test.ttf", sizeFont), color, "test.ttf", sizeFont);
	GApp->addFont(f);

	Text *text = new Text(GApp->getFonts().at(0), " ");
	text->setPosX(20);  text->setPosY(20);
	GApp->addText(text);

	Text *textY = new Text(GApp->getFonts().at(0), " ");
	textY->setPosX(80);  textY->setPosY(20);
	GApp->addText(textY);

	delete f;
	delete text;
	delete textY;
}

void CGameApplication::renderApplication()
{		
	glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); 
	glEnable(GL_TEXTURE_2D);
	glEnable(GL_BLEND);
	
	vector<ImagePtr> tempImages = GApp->getImages();
	vector<ImagePtr>::iterator iterImage;

	for (iterImage = tempImages.begin(); iterImage != tempImages.end(); ++iterImage)
	{
		if ( (*iterImage)->getIsActive() )
		{
			CImageM->drawSprites( (*iterImage)->getTexture(), (*iterImage)->getPosX(), (*iterImage)->getPosY(), 
								  (*iterImage)->getOffsetX(), (*iterImage)->getOffsetY() );
		}
	}

	vector<TextPtr> tempTexts = GApp->getTexts();
	vector<TextPtr>::iterator iterText;

	for (iterText = tempTexts.begin(); iterText != tempTexts.end(); ++iterText)
	{
		CTextM->drawFonts( (*iterText) );
	}

	CMapM->drawMap(GApp->getCurrentMap());

	SpritePtr spr = GApp->getPlayer()->getSpritePlayer();
	CImageM->drawSprites( spr->getTexture(), spr->getPosX(), spr->getPosY(), 
			spr->getPosX() + spr->getWidth(), spr->getPosY() + spr->getHeight());

	glDisable(GL_BLEND);
	glDisable(GL_TEXTURE_2D);

}

void CGameApplication::glEnable2D()
{
	int vPort[4];
  
	glGetIntegerv(GL_VIEWPORT, vPort);
  
	glMatrixMode(GL_PROJECTION);
	glPushMatrix();
	glLoadIdentity();
  
	glOrtho(0, vPort[2], 0, vPort[3], -1, 1);
	glMatrixMode(GL_MODELVIEW);
	glPushMatrix();
	glLoadIdentity();
}

void CGameApplication::glDisable2D()
{
	glMatrixMode(GL_PROJECTION);
	glPopMatrix();   
	glMatrixMode(GL_MODELVIEW);
	glPopMatrix();	
}
